﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class Employee_ChangeVacancy : System.Web.UI.Page
{
    string VacancyID = "";
    string UserName = "";
    EmployeeMethord EM = new EmployeeMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            UserName = Session["User"].ToString();
            VacancyID = Request.QueryString["ID"].ToString();
            txtDateFilfilled_CalendarExtender.StartDate = DateTime.Now;
            if (!IsPostBack)
            {
                LoadData(VacancyID);
            }
        }
        catch (Exception ex)
        {
        }
    }
    public void LoadData(string _VacancyID)
    {
        try
        {
            DataTable tb = EM.ReturnInformationVacancy(_VacancyID, UserName);
            if (tb.Rows.Count > 0)
            {
                DataRow item = tb.Rows[0];
                txtVacancyID.Text = item["VacancyID"].ToString();
                LoadTypeJob(item["JobID"].ToString());
                txtTitle.Text = item["Title"].ToString();
                txtDecription.Text = item["Description"].ToString();
                txtNumberofJob.Text = item["NumberOfJob"].ToString();
                txtNumEmpty.Text = item["NumberEmpty"].ToString();
                txtSalary.Text = item["Salary"].ToString();
                txtAddress.Text = item["Workplace"].ToString();
                txtDateCreation.Text = item["DateCreatioin"].ToString().Substring(0, item["DateCreatioin"].ToString().IndexOf(" "));
                txtDateFilfilled.Text = item["DateFulfilled"].ToString().Substring(0, item["DateFulfilled"].ToString().IndexOf(" "));
                for (int i = 0; i < 3; i++)
                {
                    ddlStatus.SelectedIndex = i;
                    if (ddlStatus.SelectedValue.Equals(item["Status"].ToString()))
                    {
                        if (ddlStatus.SelectedValue.Equals("3"))
                        {
                            ddlStatus.Enabled = false;
                            btnSaveChange.Enabled = false;
                            btnClear.Enabled = false;
                        }
                        break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    private void LoadTypeJob(string _JobID)
    {
        SqlDataReader dr = EM.ReturnTypeOfJob();
        ddlTypeJob.DataSource = dr;
        ddlTypeJob.DataTextField = "JobName";
        ddlTypeJob.DataValueField = "JobID";
        ddlTypeJob.DataBind();
        EM.connClose();
        for (int i = 0; i < ddlTypeJob.Items.Count; i++)
        {
            ddlTypeJob.SelectedIndex = i;
            if (ddlTypeJob.SelectedValue.Equals(_JobID))
            {
                break;
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtTitle.Text = "";
        txtDecription.Text = "";
        txtNumberofJob.Text = "";
        txtSalary.Text = "";
        txtAddress.Text = "";
        txtDateFilfilled.Text = "";
        lblSuccess.Text = "";
        lblError.Text = "";
    }
    protected void btnSaveChange_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        if (txtTitle.Text.Equals(""))
        {
            lblTitleError.Text = "(*)";
            lblError.Text = "Title Vacancy can't be null";
        }
        else if (txtDecription.Text.Equals(""))
        {
            lblTitleError.Text = "";
            lblDescriptError.Text = "(*)";
            lblError.Text = "Description can't be null";
        }
        else if (txtNumberofJob.Text.Equals(""))
        {
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "(*)";
            lblError.Text = "Number of job can't be null";
        }
        else if (txtSalary.Text.Equals(""))
        {
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblErrorSalary.Text = "(*)";
            lblError.Text = "Salaries can't be null";
        }
        else if (txtAddress.Text.Equals(""))
        {
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblErrorSalary.Text = "";
            lblErrorAddress.Text = "(*)";
            lblError.Text = "Address can't be null";
        }
        else if (txtDateFilfilled.Text.Equals(""))
        {
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblErrorSalary.Text = "";
            lblErrorAddress.Text = "";
            lblDateError.Text = "(*)";
            lblError.Text = "Date fulfilled can't be null";
        }
        else
        {
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblErrorSalary.Text = "";
            lblErrorAddress.Text = "";
            lblDateError.Text = "";
            lblError.Text = "";
            try
            {
                DateTime date = new DateTime();
                try 
                {	        
		            date = Convert.ToDateTime(txtDateFilfilled.Text);
                }
                catch (Exception)
                {
                    lblDateError.Text = "(*)";
                    lblError.Text = "Check value of DataFulfilled";
                }
                int number = Convert.ToInt32(txtNumberofJob.Text);
                bool result = EM.UpdateVacancy(txtVacancyID.Text, txtTitle.Text, Convert.ToInt32(ddlTypeJob.SelectedValue), txtDecription.Text, number, txtSalary.Text, txtAddress.Text, date, Convert.ToInt32(ddlStatus.SelectedValue));
                if (result)
                {
                    LoadData("V0001");
                    lblSuccess.Text = "Update Success!";
                }
                else
                {
                    lblError.Text = "Faild";
                }
            }
            catch (Exception ex)
            {
                lblNumber.Text = "(*)";
                lblError.Text = "Number of job must be a number";
                Console.Write(ex.Message);
            }
        }
    }
}